﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using JiebaNet.Segmenter;
using Newtonsoft.Json;

namespace DailyDiet.Services.Impl
{
    public class SplitSentenceService : ISplitSentenceService {
        private JiebaSegmenter _segmenter;
        private IPreferenceStorage _preference;

        private const string _dirName = "JiebaResource";

        public static readonly string DirPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder
                .LocalApplicationData), _dirName);

        public JiebaSegmenter Segmenter {
            get {
                JiebaNet.Segmenter.ConfigManager.ConfigFileBaseDir = DirPath;
                _segmenter = new JiebaSegmenter();
                return _segmenter;
            }
        }


        public List<string> SplitSentenceToWords(string sentence) {
            List<string> words = new List<string>();
            if (sentence == null || sentence.Equals("")) {
                return words;
            }

            words.AddRange(Segmenter.Cut(sentence));
            return words;
        }

        public bool JiebaIsInitialize() {
            return _preference.Get(SplitSentenceConstant.preferenceKey,
                SplitSentenceConstant.Default) == SplitSentenceConstant.Version;
        }

        public async Task InitializeJieba() {
            System.IO.Directory.CreateDirectory(DirPath);
            foreach (var file in SplitSentenceConstant.files) {
                using (var embeddedResourceStream =
                    Assembly.GetExecutingAssembly().GetManifestResourceStream(file))
                using (var fileStream =
                    new FileStream(Path.Combine(DirPath,file), FileMode.OpenOrCreate))
                {
                    if (embeddedResourceStream == null || fileStream == null)
                    {
                        throw new Exception();
                    }
                    await embeddedResourceStream.CopyToAsync(fileStream);
                }
            }

            _preference.Set(SplitSentenceConstant.preferenceKey,
                SplitSentenceConstant.Version);
        }


        public SplitSentenceService(IPreferenceStorage preferenceStorage) {
            _preference = preferenceStorage;
        }
    }

    public static class SplitSentenceConstant {
        public const string preferenceKey =
            nameof(SplitSentenceConstant) + nameof(preferenceKey);

        public const int Default = -1;

        public const int Version = 1;

        public static readonly List<string> files = new List<string>() {
            "char_state_tab.json",
            "cn_synonym.txt",
            "dict.txt",
            "idf.txt",
            "pos_prob_emit.json",
            "pos_prob_start.json",
            "pos_prob_trans.json",
            "prob_emit.json",
            "prob_trans.json",
            "stopwords.tx"
        };
    }
}
